package guoanmaker.operator.business.controller;

import java.util.concurrent.Semaphore;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import guoanmaker.operator.business.dao.MonitoringParamsDTO;
import guoanmaker.operator.business.service.OperatorToutiaoADDeviceService;
import guoanmaker.operator.helper.common.AjaxJson;
import io.swagger.annotations.ApiOperation;

/**
 * 
* @ClassName: OperatorTouTiaoUserADController.java 
* @Description: 评价controller
* @author Cancerl
* @date 2018年5月11日
*  
 */
@RestController
@RequestMapping("/toutiaoAD")
public class OperatorTouTiaoUserADController {
	
	Semaphore semaphore=new Semaphore(1);  //定义资源的总数量
	
	@Autowired
	private OperatorToutiaoADDeviceService operatorToutiaoADDeviceService;
	
	@ApiOperation(value="保存注册设备号",notes="保存注册设备号")
	@PostMapping("saveUserDevice")
	////客户端类型,0-Android,1-IOS,2-WP,3-Others
	////interfaceType 1>>激活   2>>注册
	public AjaxJson saveUserDevice(String deviceid,Integer type,String username,Integer interfaceType){
		AjaxJson ajaxJson = new AjaxJson();
		try {
			try {
	            semaphore.acquire();  //请求占用一个资源
	            ajaxJson.setSuccess(true).setStatus(200).setData(operatorToutiaoADDeviceService.saveUserDevice(deviceid, type, username,interfaceType));
	        } catch (InterruptedException e) {
	            e.printStackTrace();
	            ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
	        }finally{
	            semaphore.release();//释放一个资源
	        }
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	
	@ApiOperation(value="头条反馈回调IOS",notes="头条反馈回调IOS")
	@GetMapping("/ttpushIos")
	public AjaxJson callbackUserDeviceIos(String adid,String cid,String mac,String timestamp,String convertid,String callback,String os,
    		String idfa,
    		String imei,String androidid,String ip){
		MonitoringParamsDTO monitoringDto = new MonitoringParamsDTO(adid,cid,mac,timestamp,convertid,callback,os,idfa,imei,androidid,ip);
		System.out.println("头条回调ios调用成功");
		System.out.println(monitoringDto);
		AjaxJson ajaxJson = new AjaxJson();
		try {
			try {
	            semaphore.acquire();  //请求占用一个资源
	            operatorToutiaoADDeviceService.saveCallbackUserDeviceInfoIos(monitoringDto);
	        } catch (InterruptedException e) {
	            e.printStackTrace();
	            ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
	        }finally{
	            semaphore.release();//释放一个资源
	        }
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	
	@ApiOperation(value="头条反馈回调Android",notes="头条反馈回调Android")
	@GetMapping("/ttpushAndroid")
	public AjaxJson callbackUserDeviceAndroid(String adid,String cid,String mac,String timestamp,String convertid,String callback,String os,
    		String idfa,
    		String imei,String androidid,String ip){
		MonitoringParamsDTO monitoringDto = new MonitoringParamsDTO(adid,cid,mac,timestamp,convertid,callback,os,idfa,imei,androidid,ip);
		System.out.println("头条回调Android调用成功");
		System.out.println(monitoringDto);
		AjaxJson ajaxJson = new AjaxJson();
		try {
			try {
	            semaphore.acquire();  //请求占用一个资源
	            operatorToutiaoADDeviceService.saveCallbackUserDeviceInfoAndroid(monitoringDto);
	        } catch (InterruptedException e) {
	            e.printStackTrace();
	            ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
	        }finally{
	            semaphore.release();//释放一个资源
	        }
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
}
